package org.rabbit.service;

import java.util.UUID;

import javax.annotation.Resource;

import org.rabbit.dao.UserDao;
import org.rabbit.pojo.User;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;

@Service
@Component
public class UserServiceimpl implements UserServive{

	@Resource
	private RabbitTemplate rt;
	
	@Resource
	private UserDao ud;
	
	@Resource
	private RedisTemplate<String,User> redisTemplate;
	
	@Override
	public void register(User u) {
		System.out.println("user register.......");
		/*for(int i=0;i<10;i++)
		//发送到默认的交换机上
		rt.convertAndSend("hello", "helloworld");*/
		
		
		//发送到fanout模式的交换机
	  // rt.convertAndSend("fanoutExchage","","2190345284@qq.com");
		rt.convertAndSend("topicExchange","f.a.c","hello");
	}

	@Override
	public String login(User u) {
		User user = ud.findUserByUname(u.getUname());
		System.out.println(user+"((((");
		String tokenId=null;
		if(user.getUpass().equals(u.getUpass())) {
			//生成tokenId
			tokenId=UUID.randomUUID().toString();
			System.out.println("tikenId:"+tokenId);
			ValueOperations<String, User> ops = redisTemplate.opsForValue();
			ops.set(tokenId, user);
		}
		return tokenId;
	}	

}
